axios.defaults.baseURL = "https://mock.apipark.cn/m1/4730770-4383288-default"
//抽取轻提示
//showToast
function showToast(msg){
    const toastDom = document.querySelector('.my-toast');
    const toast = new bootstrap.Toast(toastDom);
    document.querySelector('.toast-body').innerHTML = msg;
    toast.show()
    //显示
}
function checkLogin(){
    const token = localStorage.getItem('token');
    if(token==null){
       showToast('非法登录，请先登录');
       setTimeout(()=>{
        location.href = './login.html'
    },1500)
}
}
//抽取渲染函数渲染那缓存中的用户名
function renderUsername(){
    const username = localStorage.getItem('username');
    document.querySelector('.username').innerHTML = username;

}
//抽取退出登录函数
function registerLoginOut(){
    document.querySelector('#logout').addEventListener('click',()=>{
        localStorage.removeItem('token');
        localStorage.removeItem('username');
        location.href = './login.html'
    })
}
//axios拦截器 请求/响应
//登录方面失效
axios.interceptors.request.use(function (config) {
    config.headers["info"] = "hi"
    console.log(config);
    const token = localStorage.getItem('token')
    if(token){
        config.headers["Authorization"] = token;
    }
    return config;
    
},function (error) {
    return Promise.reject(error)

})
//响应方面失效
axios.interceptors.response.use(function (response) {
    return response.data;
    
},function(){
    if(error.response.status==401){
        localStorage.removeItem('token');
        localStorage.removeItem('username');
        showToast('登录失效，请重新登录');
        setTimeout(function(){
            location.href = './login.html'
            
        },1500)
    }
    return Promise.reject(error);
})